<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        style="width: 200px;"
        class="filter-item"
        placeholder="请输入摄像机编号"
        v-model="queryForm.code"
      >
      </el-input>
      <el-input
        style="width: 200px;"
        class="filter-item"
        placeholder="请输入摄像机名称"
        v-model="queryForm.name"
      >
      </el-input>
      <el-input
        style="width: 200px;"
        class="filter-item"
        placeholder="请输入区域名"
        v-model="queryForm.regionName"
      >
      </el-input>
      <el-input
        style="width: 200px;"
        class="filter-item"
        placeholder="请输入区域编号"
        v-model="queryForm.regionCode"
      >
      </el-input>
      <el-input
        style="width: 200px;"
        class="filter-item"
        placeholder="请输入小区名"
        v-model="queryForm.communityName"
      >
      </el-input>
      <el-select
        clearable
        style="width: 160px"
        class="filter-item"
        v-model="queryForm.type"
        placeholder="请选择摄像机类型"
      >
        <el-option
          v-for="(val, key) in Dicts.CAMERA_TYPE"
          :key="key"
          :label="val"
          :value="key"
        >
        </el-option>
      </el-select>
      <el-select
        clearable
        style="width: 160px"
        class="filter-item"
        v-model="queryForm.connectStatus"
        placeholder="请选择摄像机状态"
      >
        <el-option
          v-for="(val, key) in Dicts.CAMERA_CONNECT_STATUS"
          :key="key"
          :label="val"
          :value="key"
        >
        </el-option>
      </el-select>
      <el-button
        class="filter-item"
        @click="doQuery"
        type="primary"
        icon="el-icon-search"
        title="搜索"
        >搜索</el-button
      >
      <el-button
        class="filter-item"
        style="margin-left: 10px;"
        @click="addCamera()"
        type="success"
        icon="el-icon-plus"
        >新增</el-button
      >
    </div>
    <el-table
      v-loading="loading"
      @sort-change="handleSortChange"
      :data="records"
      style="width: 100%"
    >
      <el-table-column prop="rowId" label="ID" width="100" align="center">
      </el-table-column>
      <el-table-column
        prop="code"
        label="摄像机编号"
        width="150"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="name"
        label="摄像机名称"
        width="150"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="outerHost"
        label="摄像机外网IP地址"
        width="200"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="outerPort"
        label="摄像机外网端口号"
        width="200"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="host"
        label="摄像机IP地址"
        width="200"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="port"
        label="摄像机端口号"
        width="200"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="type"
        label="摄像机类型"
        width="150"
        align="center"
      >
        <template slot-scope="scope">
          {{ Dicts.CAMERA_TYPE[scope.row.type] }}
        </template>
      </el-table-column>
      <el-table-column
        prop="position"
        label="摄像机位置"
        width="150"
        align="center"
      >
        <template slot-scope="scope">
          {{ Dicts.CAMERA_POSITION[scope.row.position] }}
        </template>
      </el-table-column>
      <el-table-column
        prop="connectStatus"
        label="在线状态"
        width="150"
        align="center"
      >
        <template slot-scope="scope">
          {{ Dicts.CAMERA_CONNECT_STATUS[scope.row.connectStatus] }}
        </template>
      </el-table-column>
      <el-table-column
        prop="regionName"
        label="区域名"
        width="200"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="regionCode"
        label="区域编号"
        width="200"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="communityName"
        label="小区名"
        width="250"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="createTime"
        label="创建时间"
        width="250"
        align="center"
      >
      </el-table-column>
      <el-table-column
        width="250"
        align="center"
        label="操作"
        class-name="small-padding"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            @click="editCameraById(scope.row)"
            title="编辑"
            icon="el-icon-edit"
            circle
          ></el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.row.rowId)"
            title="删除"
            icon="el-icon-delete"
            circle
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryForm.current"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="queryForm.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="queryForm.total"
      >
      </el-pagination>
    </div>

    <el-dialog
      :title="`${addForm.rowId ? '编辑' : '新增'}摄像机设备`"
      :visible.sync="dialogFormVisible"
      width="40%"
      :close-on-click-modal="false"
    >
      <el-form
        :model="addForm"
        label-width="140px"
        :rules="addFormRules"
        ref="addForm"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="摄像机编号:" prop="code">
              <el-input v-model="addForm.code" auto-complete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="摄像机名称:" prop="name">
              <el-input v-model="addForm.name" auto-complete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="摄像机外网IP地址:" prop="outerHost">
              <el-input
                v-model="addForm.outerHost"
                auto-complete="off"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="摄像机外网端口号:" prop="outerPort">
              <el-input
                v-model="addForm.outerPort"
                auto-complete="off"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="摄像机密码:" prop="password">
              <el-input
                v-model="addForm.password"
                auto-complete="off"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="摄像机端口号:" prop="port">
              <el-input v-model="addForm.port" auto-complete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="摄像机IP地址:" prop="host">
              <el-input v-model="addForm.host" auto-complete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="摄像机纬度:" prop="lat">
              <el-input v-model="addForm.lat" auto-complete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="摄像机经度:" prop="lng">
              <el-input v-model="addForm.lng" auto-complete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="摄像机类型" prop="type">
              <el-select
                v-model="addForm.type"
                placeholder="摄像机类型"
                style="width: 100%;"
              >
                <el-option
                  v-for="(val, key) in Dicts.CAMERA_TYPE"
                  :key="key"
                  :label="val"
                  :value="key"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="布防区域" prop="distributionControlId">
              <el-select
                v-model="addForm.distributionControlId"
                placeholder="布防区域"
                style="width: 100%;"
              >
                <el-option
                  v-for="(item, index) in allRegionName"
                  :key="index"
                  :label="item.regionName"
                  :value="item.rowId"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="摄像机位置" prop="position">
              <el-select
                v-model="addForm.position"
                placeholder="摄像机位置"
                style="width: 100%;"
              >
                <el-option
                  v-for="(val, key) in Dicts.CAMERA_POSITION"
                  :key="key"
                  :label="val"
                  :value="key"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="在线状态" prop="connectStatus">
              <el-select
                v-model="addForm.connectStatus"
                placeholder="在线状态"
                style="width: 100%;"
              >
                <el-option
                  v-for="(val, key) in Dicts.CAMERA_CONNECT_STATUS"
                  :key="key"
                  :label="val"
                  :value="key"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveCamera(addForm.rowId)"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { queryCameraList, addOneCamera, deleteCamera } from '@/api/device'
import { selectAllRegionName } from '@/api/consumer'

export default {
  name: 'CameraList',
  data() {
    return {
      dialogFormVisible: false,
      loading: false,
      exampleForm: {
        rowId: undefined
      },
      modifyForm: {},
      exampleFormRules: {},
      queryForm: {
        current: 1,
        pages: 0,
        total: 0,
        size: 10,
        ascs: '',
        descs: '',
        importance: undefined,
        title: undefined
      },
      allRegionName: [],
      addForm: {},
      addFormRules: {
        code: [
          { required: true, message: '请填写摄像机编号', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请填写摄像机名称', trigger: 'blur' }
        ],
        outerHost: [
          { required: true, message: '请填写摄像机外网IP地址', trigger: 'blur' }
        ],
        outerPort: [
          { required: true, message: '请填写摄像机外网端口号', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请填写摄像机密码', trigger: 'blur' }
        ],
        host: [
          { required: true, message: '请填写摄像机IP地址', trigger: 'blur' }
        ],
        port: [
          { required: true, message: '请填写摄像机端口号', trigger: 'blur' }
        ],
        type: [
          { required: true, message: '请选择摄像机类型', trigger: 'blur' }
        ],
        position: [
          { required: true, message: '请选择摄像机位置', trigger: 'blur' }
        ],
        connectStatus: [
          { required: true, message: '请选择在线状态', trigger: 'blur' }
        ],
        distributionControlId: [
          { required: true, message: '请填写布防区域', trigger: 'blur' }
        ]
      },
      records: []
    }
  },
  created() {
    this.doQuery()
  },
  methods: {
    handleSortChange({ column, prop, order }) {
      this.queryForm.ascs = ''
      this.queryForm.descs = ''
      if (order) {
        if (order === 'ascending') {
          this.queryForm.ascs = column.sortBy
        } else {
          this.queryForm.descs = column.sortBy
        }
      }
      this.queryForm.current = 0
      this.doQuery()
    },
    handleCurrentChange(current) {
      this.queryForm.current = current
      this.doQuery()
    },
    handleSizeChange(size) {
      this.queryForm.size = size
      this.doQuery()
    },
    doQuery() {
      this.loading = true
      queryCameraList(this.queryForm).then(({ data: { data } }) => {
        this.queryForm.current = data.current
        this.queryForm.pages = data.pages
        this.queryForm.total = data.total
        this.queryForm.size = data.size
        this.records = data.records
        this.loading = false
      })
    },
    addCamera() {
      this.addForm = {}
      this.allRegionName = []
      this.dialogFormVisible = true
      selectAllRegionName().then(({ data: { data } }) => {
        this.allRegionName = data
      })
      this.$refs.addForm && this.$refs.addForm.resetFields()
    },
    saveCamera() {
      this.$refs.addForm.validate(valid => {
        if (valid) {
          addOneCamera(this.addForm).then(({ data: { data, msg, code } }) => {
            if (code === 0) {
              this.doQuery()
              this.dialogFormVisible = false
            }
            this.$message({
              showClose: true,
              message: msg,
              type: code === 0 ? 'success' : 'error'
            })
          })
        } else {
          return false
        }
      })
    },
    editCameraById({
      rowId,
      code,
      name,
      outerHost,
      outerPort,
      port,
      host,
      lat,
      lng,
      password,
      distributionControlId,
      type,
      position,
      connectStatus
    }) {
      this.addCamera()
      this.addForm = {
        rowId: rowId,
        code: code,
        name: name,
        outerHost: outerHost,
        outerPort: outerPort,
        password: password,
        port: port,
        host: host,
        lat: lat,
        lng: lng,
        distributionControlId: distributionControlId,
        type: type,
        position: position,
        connectStatus: connectStatus
      }
    },
    handleDelete(rowId) {
      this.$confirm('此操作将永久删除该设备, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteCamera(rowId).then(({ data: { data, msg } }) => {
          this.doQuery()
          this.$message({
            showClose: true,
            message: msg,
            type: 'success'
          })
        })
      })
    }
  },
  components: {}
}
</script>

<style scoped></style>
